package com.googlecode.gwt.test.plugin.editors;

import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

public class WordTracker {

	private List<String> wordBuffer;
	
	KeyWords keyWords;

	public WordTracker(KeyWords keyWords) {
		if(wordBuffer==null){
			wordBuffer=new LinkedList<String>();
		}
		this.keyWords=keyWords;
	}

	class Proposal{
		String displayedText;
		String replacementText;
		public Proposal(String displayedText) {
			this.displayedText=displayedText;
		}
		private Proposal(String displayedText, String replacementText) {
			this.displayedText = displayedText;
			this.replacementText = replacementText;
		}
		public String getDisplayedText() {
			return displayedText;
		}
		public void setDisplayedText(String displayedText) {
			this.displayedText = displayedText;
		}
		public String getReplacementText() {
			return replacementText;
		}
		public void setReplacementText(String replacementText) {
			this.replacementText = replacementText;
		}
	}

	public List<Proposal> suggest(String word) {
		String begins="";
		String [] tab=null;
		if(word.contains(";")){
			tab=word.split(";");
			begins=word.substring(0,word.length()-tab[tab.length-1].length());
			word=tab[tab.length-1];
		}
		wordBuffer.clear();
		if(tab==null){
			for(String s:keyWords.getKEYWORDS())
				wordBuffer.add(s);
			for(String s:keyWords.getCustomKeywords())
				wordBuffer.add(s);
		}
		else if(tab.length==2){
			for(String s:keyWords.getCUSTOM_MACROS())
				wordBuffer.add(s);
		}
		Collections.sort(wordBuffer);
		 List<Proposal> suggestions = new LinkedList<Proposal>();
		    for (Iterator<String> i = wordBuffer.iterator(); i.hasNext();) {
		      String currWord = (String) i.next();
		      if (currWord.toLowerCase().startsWith(word.toLowerCase())) {
					suggestions.add(new Proposal(currWord,begins+currWord));
		      }
		    }
		 return suggestions;
	}

}
